package com.example.controller;

import com.example.common.Result;
import com.example.pojo.entity.Order;
import com.example.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    /*
     * 新增订单
     * */
    @PostMapping("/add")
    public Result add(@RequestBody Order order){
        orderService.insert(order);
        return Result.success();
    }
    /*
          修改订单
         */
    @PutMapping("/update")
    public Result update(@RequestBody Order order){
        orderService.updateOrder(order);
        return Result.success();
    }


    /*
         删除订单
         */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        orderService.removeById(id);
        return Result.success();
    }



    //批量删除订单

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        orderService.removeBatchByIds(ids);
        return Result.success();
    }


    // 查询所有订单

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Order> list = orderService.list();
        return Result.success(list);
    }


    //根据ID查询订单

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Order order = orderService.getById(id);
        return Result.success(order);
    }

    //根据订单号查询订单
    @GetMapping("/selectByOrderNum/{orderNum}")
    public Result selectByOrderNum(@PathVariable String orderNum){
        Order order = orderService.getByOrderNum(orderNum);
        return Result.success(order);
    }


    // 分页查询订单

    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String goodsName,
            @RequestParam(defaultValue = "") Integer userId) {
        PageInfo<Order> pageInfo = orderService.selectByPage(pageNum, pageSize, goodsName, userId);
        return Result.success(pageInfo);
    }
}
